package edu.sctu.xialei.june.day20150628two_test.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2015/6/23.
 */
public class UrlMethods {

    public String urlPOST(String path,String string) throws IOException {

        URL url = new URL(path);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(string.getBytes());

        InputStream inputStream = httpURLConnection.getInputStream();
        byte value[] = new byte[10240];
        inputStream.read(value);

        return new String(value);
    }

    public String urlGET(String urlPath) throws IOException {

        URL url = new URL(urlPath);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        httpURLConnection.setDoInput(true);

        InputStream inputStream = (InputStream) httpURLConnection.getInputStream();
        byte[] value = new byte[1024];
        int count = 0;
        String json = "";

        while ((count = inputStream.read(value)) != -1) {
            json = json + new String(value,0, count,"utf-8");
        }

        inputStream.close();

        return json;
    }
}
